package com.cg.context;

import com.alibaba.ttl.TransmittableThreadLocal;

import java.util.Optional;

public class UserContext {

    public static final ThreadLocal<User> USER = new TransmittableThreadLocal<>();


    public User getContext() {
        return USER.get();
    }

    public static String getUserId() {
        return Optional.ofNullable(USER.get()).map(o -> o.getUserId()).orElse(null);
    }

    public static String getUserName() {
        return Optional.ofNullable(USER.get()).map(o -> o.getUserName()).orElse(null);
    }

    public static String getUserPhone() {
        return Optional.ofNullable(USER.get()).map(o -> o.getUserPhone()).orElse(null);
    }


    public static void setUser(User user) {
        USER.set(user);
    }

    public static void clear(){
        USER.remove();
    }

}
